package com.example.leoss.M_wode;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.media.AudioManager;
import android.os.Build;

import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.example.leoss.R;

import jp.wasabeef.glide.transformations.BlurTransformation;

public class MyMusicPlayerView extends RelativeLayout {


    private ImageView my_music_background;
    private ImageView my_music_image;
    private ImageView my_music_start;
    private ObjectAnimator myAnimator;
    private String imageUrl;
    private Context context;


    public MyMusicPlayerView(Context context) {
        super(context);
        this.context=context;
        findView(context);

    }


    public MyMusicPlayerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyMusicPlayerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        findView(context);
        initAudioManager();

    }



    private void initAudioManager() {
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    }

    private void findView(Context context) {
        View view = View.inflate(context, R.layout.my_music_player, this);
        initView(view);

//
//        initAnimation();
    }


    private void initView(View view) {
        my_music_background = (ImageView) view.findViewById(R.id.my_music_background);
        my_music_image = (ImageView) view.findViewById(R.id.my_music_image_p1);


    }
    private void initAnimation()
    {
        myAnimator = ObjectAnimator.ofFloat(my_music_image, "rotation", 0f, 360f);
        myAnimator.setDuration(25000);
        myAnimator.setRepeatCount(-1);
        myAnimator.setInterpolator(new LinearInterpolator());
        myAnimator.start();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            myAnimator.pause();
        } else
        {
            myAnimator.cancel();
        }
    }

    /**
     * 开始动画
     */
    private void startMyAnimation()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            myAnimator.resume();
        } else {
            myAnimator.start();
        }
    }

    /**
     * 停止动画
     */
    private void stopAnimation() {
        if (myAnimator != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                myAnimator.pause();
            } else {
                myAnimator.cancel();
            }
        }
    }

    public void setImage1(String imageUrl)
    {


        this.imageUrl = imageUrl;
        //高斯模糊背景
        Glide.with(context)
                .load(TextUtils.isEmpty(imageUrl) ? R.drawable.music_play_icon : imageUrl)
                .bitmapTransform(new BlurTransformation(context, 30, 3))
                .dontAnimate()
                .into(my_music_background);

        //音乐封面
        Glide.with(context)
                .load(TextUtils.isEmpty(imageUrl) ? R.drawable.music_play_icon : imageUrl)
                .transform(new GlideCircleUtil(context))
                .dontAnimate()
                .into(my_music_image);

    }

}


